import 'package:flutter/material.dart';
import 'package:flutter_xc/dao/travel_tab_dao.dart';
import 'package:flutter_xc/model/travel_tab_model.dart';
import 'package:flutter_xc/pages/travel_tab_page.dart';

class TravelPage extends StatefulWidget {
  /// 重写 createState 方法 并返回一个
  /// __TabNavigatorState类型的
  @override
  _TravelPageState createState() => _TravelPageState();
}

// 实现一个State,
class _TravelPageState extends State<TravelPage> with TickerProviderStateMixin {
  TabController _controller;
  List<TravelTab> _tabs = [];
  TravelTabModel travelTabModel;

  // 请求接口
  @override
  void initState() {
    // 这里用的是tabcontroller控制器
    // 这里TickerProviderStateMixin  不太明白
    _controller = TabController(length: 0, vsync: this);
    TravelTabDao.fetch().then((TravelTabModel model) {
      _controller = TabController(length: model.tabs.length, vsync: this);
      setState(() {
        _tabs = model.tabs;
        travelTabModel = model;
      });
    }).catchError((e) {
      print(e);
    });
    super.initState();
  }

  @override
  void dispose() {
    // 页面销毁的时候销毁controller
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
          children: <Widget>[
            Container(
              color: Colors.white,
              padding: EdgeInsets.only(top: 30),
              child: TabBar(
                controller: _controller,
                isScrollable: true,
                labelColor: Colors.black,
                labelPadding: EdgeInsets.fromLTRB(20, 0, 10, 5),
                indicator: UnderlineTabIndicator(
                  insets: EdgeInsets.only(bottom: 10),
                  borderSide: BorderSide(
                      width: 3,
                      color: Color(0xff2fcfbb)
                  ),
//                strokeCap: StrokeCap.round
                ),
                tabs: _tabs.map<Tab>((TravelTab tab) {
                  return Tab(
                    text: tab.labelName,
                  );
                }).toList(),
              ),
            ),
            Flexible(
              child: TabBarView(
                controller: _controller,
                children: _tabs.map((TravelTab tab) {
                  return TravelTabPage(
                    travelUrl: travelTabModel.url,
                    params: travelTabModel.params,
                    groupChannelCode: tab.groupChannelCode,
                  );
                }).toList(),
              ),
            )
          ],
      )
    );
  }
}